package com.softer.wxzj.service.impl;

import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.entity.ExpPayReq;
import com.softer.wxzj.entity.ExpProjectReq;
import com.softer.wxzj.entity.ExpPublic;
import com.softer.wxzj.entity.FileVO;
import com.softer.wxzj.mapper.ExpPayReqMapper;
import com.softer.wxzj.mapper.ExpProjectReqMapper;
import com.softer.wxzj.mapper.ExpPublicMapper;
import com.softer.wxzj.service.ExpPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2020/7/28
 */
@Service
public class ExpPublicServiceImpl implements ExpPublicService {
    @Autowired
    private ExpPublicMapper expPublicMapper;
    @Autowired
    private FileServiceImpl fileService;
    @Autowired
    private ExpProjectReqMapper expProjectReqMapper;
    @Autowired
    private ExpPayReqMapper expPayReqMapper;

    /**
     * 新增应急公示
     *
     * @param expPublic 公示信息
     * @return 新增结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult add(ExpPublic expPublic) {
        int num = expPublicMapper.add(expPublic);
        if (num > 0) {
            //处理图档
            CommonResult result = fileService.updateFile(expPublic.getFileTypeList(), expPublic.getProjectId());
            if (ShiroResultEnum.FAIL.getCode() == result.getData()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), result.getMessage());
            }
        }
        return CommonResult.ResponseResult(num);
    }

    /**
     * 查询应急公示信息
     *
     * @param projectId 项目ID
     * @param arcType   图档类型
     * @return 公示信息
     */
    @Override
    public ExpPublic getInfo(String projectId, String arcType) {
        ExpPublic info = expPublicMapper.getInfo(projectId);
        String state = NumStrEnum.ZERO.getNum();
        if (info != null) {
            // 查询图档信息
            List<FileVO> fileVOList = fileService.getFileVOList(projectId, arcType);
            info.setFileTypeList(fileVOList);
            ExpProjectReq projectReq = expProjectReqMapper.getInfoById(projectId);
            //查询决算拨款的状态
            int payCount = projectReq.getPayCount();
            if (projectReq.getIsLast() == 1) {
                //如果存在质保金拨款，则倒数第二笔是决算拨款
                payCount -= 1;
            }
            ExpPayReq expPayReq = expPayReqMapper.getLastSumById(projectId, payCount);
            if (expPayReq != null) {
                String flowState = expPayReq.getFlowState();
                if (NumStrEnum.ZERO.getNum().equals(flowState) || NumStrEnum.THREE.getNum().equals(flowState)) {
                    state = NumStrEnum.ONE.getNum();
                }
            }
            info.setState(state);
        }
        return info;
    }
}
